<?php
/**
 * JosemUserRepo.php
 *
 * @copyright   2024 jsjsem.cn - All Rights Reserved
 * @link        https://www.jsjsem.cn/
 * @author      stiffer.chen <service@josen.info>
 * @created     2024-04-19 14:43:15
 * @modified    2024-04-19 14:43:15
 *
 */

namespace App\Repositories;

use App\Models\UserModel;

class JosemUserRepo
{
    public static function getInfoByMobile(string $mobile) {
        $user = UserModel::query()->where("mobile", $mobile)->first();
        if (empty($user)) {
            throw new \Exception("用户信息不存在(手机号: {$mobile})");
        }
        return $user;
    }

    public static function exist(string $mobile)
    {
        try {
            self::getInfoByMobile($mobile);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    public static function resetPassword($mobile, $password)
    {
        $info = self::getInfoByMobile($mobile);
        if (empty($info)) {
            throw new \Exception("用户信息不存在(手机号: {$mobile})");
        }
        $userName = $info->username;
        $newPassword = get_password($password . $userName);

        UserModel::query()->where('mobile', $mobile)->update(['password' => $newPassword]);
    }
}